package com.example.demo.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.config.Constent;
import com.example.demo.config.Constent.SEQUENCE;
import com.example.demo.dao.ShopMapper;
import com.example.demo.dao.UserShopRelMapper;
import com.example.demo.entity.Shop;
import com.example.demo.entity.UserShopRel;
import com.example.demo.exception.SPIPException;
import com.example.demo.util.ConverterUtil;
import com.example.demo.util.FileUtil;
import com.example.demo.util.JwtUtil;
import com.google.common.base.Objects;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class ShopService {
	@Autowired
	ShopMapper shopMapper;
	@Autowired
	UserShopRelMapper userShopRelMapper;

	@Transactional(rollbackFor=Exception.class)
	public void add(Shop shop) throws SPIPException {
		shop.setOperator();
		shopMapper.insertSelective(shop);
		UserShopRel userShopRel = new UserShopRel();
		userShopRel.setUserId(ConverterUtil.toLong(JwtUtil.getUserId()));
		userShopRel.setShopId(shop.getId());
		userShopRel.setOperator();
		userShopRelMapper.insert(userShopRel);
	}
	
	@Transactional(rollbackFor=Exception.class)
	public void edit(Shop shop,MultipartFile file) throws SPIPException {
		String userId = JwtUtil.getUserId();
		if(shop.getId()==null) {
			add(shop);
		}else {
			shop.setUpdater(userId);
			shop.setUpdateDttm(new Date());
			shopMapper.updateByPrimaryKey(shop);
		}
		setCurrentShop(userId, shop);
		if (file!=null&&!file.isEmpty()) {
			FileUtil.upload(file, Constent.getFileUpUrl(SEQUENCE.SHOP, shop.getId()));
		}
	}

	public void setCurrentShop(String userId,Shop shop ) throws SPIPException {
		if(Objects.equal(shop.getIsCurrentShop(), 1)) {
			UserShopRel record = new UserShopRel();
			record.setIsCurrentShop(0);
			Example example= new Example(UserShopRel.class);
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("userId", userId);
			userShopRelMapper.updateByExampleSelective(record, example);
			
			UserShopRel userShopRel = new UserShopRel();
			userShopRel.setUserId(ConverterUtil.toLong(userId));
			userShopRel.setShopId(shop.getId());
			userShopRel.setIsCurrentShop(1);
			userShopRel.setUpdater(JwtUtil.getUserId());
			userShopRel.setUpdateDttm(new Date());
			userShopRelMapper.updateByPrimaryKeySelective(userShopRel);
		}else {
			UserShopRel userShopRel = new UserShopRel();
			userShopRel.setUserId(ConverterUtil.toLong(userId));
			userShopRel.setShopId(shop.getId());
			userShopRel.setIsCurrentShop(0);
			userShopRel.setUpdater(JwtUtil.getUserId());
			userShopRel.setUpdateDttm(new Date());
			userShopRelMapper.updateByPrimaryKeySelective(userShopRel);
		}
	}
}
